<?php


namespace app\api\controller\v1;
use app\controller\Base;
use app\model\Article;
use think\exception\ValidateException;
use think\facade\Validate;
use app\validate\Article as ArticleValidate;
use think\Request;

/**
 * Class Index
 * @package app\controller
 */
class Index extends Base
{
    /**
     * @return string
     */
    public function index()
    {
        return 'thinkphp6 api';
    }


    //多数据分页接口

    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function AllDb(){

        $data = Article::field('id,user_id,title,title_img,pv,content')
            ->page($this->page,$this->pageSize)->select();

        if ($data->isEmpty()){
            return $this->create($data,'没有数据',204);
        }else{
            return $this->create($data,'请求成功',200);
        }
    }

    //获取单一数据

    /**
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function read($id){

        //验证输入的参数合法性，参数为整型
        if (!Validate::isInteger($id)){
            return $this->create([],'参数不合法',400);
        }

        $data = Article::find($id);

        if (empty($data)){
            return $this->create($data,'没有数据',204);
        }else{
            return $this->create($data,'请求成功123',200);
        }

    }


    //新增数据api

    /**
     * @param Request $request
     * @return \think\Response
     */
    public function save(Request $request){

        //获取表单数据
        $data = $request->param();

        //验证返回
        try {
            Validate(ArticleValidate::class)->check($data);

        }catch (ValidateException $exception){
            //错误返回
            return $this->create([],$exception->getError(),400);
        }

        //写入数据并返回ID
        $id = Article::create($data)->getData('id');

        if (empty($id)){
            return $this->create([],'写入失败',400);
        }else{
            return $this->create($id,'写入成功',200);
        }


    }

    //删除数据api

    /**
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id){

        if(!Validate::isInteger($id)){
            return $this->create([],'参数不合法~',400);
        }

        //删除

        try {
            Article::find($id)->delete();//返回布尔值
            return $this->create([],'删除成功',200);
//            dd();
        }catch (\Error $e){
            return $this->create([],'删除失败',400);
        }

    }


    //更新数据api
    public function update(Request $request,$id){

//      获取数据
        $data = $request->param();

//        var_dump($data);

        //验证返回，scene定义场景
        try {
            Validate(ArticleValidate::class)->scene('eidt')->check($data);

        }catch (ValidateException $exception){
            //错误返回
            return $this->create([],$exception->getError(),400);
        }

        //先查询是否存在记录
        $chkData = Article::find($id);

        if (empty($chkData)){
            return $this->create([],'操作失败',204);
        }else{
//            修改并返回ID
            $rid = Article::update($data)->getData('id');
            return $this->create($rid,'修改成功',200);
        }

    }






}